package edu.cmu.cs.webapp.ioms.ui.providers.finreport;

import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ITableColorProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;

import edu.cmu.cs.webapp.ioms.models.util.FinancialRecord;
import edu.cmu.cs.webapp.ioms.models.util.MonthRecord;
import edu.cmu.cs.webapp.ioms.models.util.QuarterRecord;
import edu.cmu.cs.webapp.ioms.models.util.YearRecord;
import edu.cmu.cs.webapp.ioms.utils.IOMSImageHelper;
import edu.cmu.cs.webapp.ioms.utils.IOMSImages;

/**
 * @author Yin Wang
 * 
 */
public class FinReportTableViewerLabelProvider extends ColumnLabelProvider
		implements ITableLabelProvider, ITableColorProvider {

	// table column index
	public static final int FIN_PERIOD = 0;
	public static final int FIN_REVENUE = 1;
	public static final int FIN_COST = 2;
	public static final int FIN_PROFIT = 3;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang
	 * .Object, int)
	 */
	@Override
	public Image getColumnImage(Object element, int columnIndex) {
		if (element instanceof FinancialRecord) {
			Image periodImg = null;
			FinancialRecord fr = (FinancialRecord) element;
			if (fr instanceof QuarterRecord) {
				periodImg = IOMSImageHelper.getInstance().getImage(
						IOMSImages.IMG_FINANCE_QUARTER);
			} else if (fr instanceof YearRecord) {
				periodImg = IOMSImageHelper.getInstance().getImage(
						IOMSImages.IMG_FINANCE_YEAR);
			} else if (fr instanceof MonthRecord) {
				periodImg = IOMSImageHelper.getInstance().getImage(
						IOMSImages.IMG_FINANCE_MONTH);
			}

			switch (columnIndex) {
			case FIN_PERIOD:
				return periodImg;
			case FIN_PROFIT:
				return IOMSImageHelper.getInstance().getImage(
						IOMSImages.IMG_FINANCE_REVENUE);
			default:
				return null;
			}
		}

		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang
	 * .Object, int)
	 */
	@Override
	public String getColumnText(Object element, int columnIndex) {
		if (element instanceof FinancialRecord) {
			FinancialRecord record = (FinancialRecord) element;
			java.text.DecimalFormat myformat = new java.text.DecimalFormat(
					"#0.00");
			switch (columnIndex) {
			case FIN_PERIOD:
				// id
				return record.getPeriod();
			case FIN_REVENUE:
				// order status
				return "" + myformat.format(record.getRevenue());
			case FIN_COST:
				// order date
				return "" + myformat.format(record.getCost());
			case FIN_PROFIT:
				// shipping date
				return "" + myformat.format(record.getProfit());

			default:
				return "";
			}
		}

		return "";

	}

	@Override
	public Color getBackground(Object element, int columnIndex) {
		return null;
	}

	@Override
	public Color getForeground(Object element, int columnIndex) {
		return null;
	}

}
